package com.example.day_17.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.day_17.R;
import com.example.day_17.adapter.HomeAdapter;
import com.example.day_17.bean.BannerBean;
import com.example.day_17.bean.InFoBean;
import com.example.day_17.contract.HomeContract;
import com.example.day_17.presenter.HomePresenter;

public class HomeFragment extends Fragment implements HomeContract.View {
    private RecyclerView mRlv;
    private HomeAdapter homeAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_home, container, false);
        initView(inflate);
        initData();
        return inflate;
    }

    private void initData() {
        HomePresenter homePresenter = new HomePresenter(this);
        homePresenter.getBannerData();
        homePresenter.getListData();
    }

    private void initView(@NonNull final View itemView) {
        mRlv = (RecyclerView) itemView.findViewById(R.id.rlv);

        mRlv.setLayoutManager(new LinearLayoutManager(getContext()));
        mRlv.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        homeAdapter = new HomeAdapter(getContext());
        mRlv.setAdapter(homeAdapter);
    }

    @Override
    public void onSuccessBanner(BannerBean bannerBean) {
        homeAdapter.setBannerDatas(bannerBean.getData());
    }

    @Override
    public void onSuccessList(InFoBean inFoBean) {
        homeAdapter.setListDatas(inFoBean.getData().getDatas());
    }

    @Override
    public void onFail(String errorMsg) {
        Log.d(TAG, "onFail: "+errorMsg);
    }

    private static final String TAG = "HomeFragment";
}
